package com.auxgroup.smarthome.job;

import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;

import javax.annotation.Resource;

/**
 * kevin chen 2017-09-14 新增通用定时调度配置类
 */
public abstract class CommonScheduler {

    @Resource
    private ZookeeperRegistryCenter regCenter;

    public JobScheduler createJobScheduler(final SimpleJob simpleJob,final String jobName,
                                           final String cron,final int shardingTotalCount,
                                           final String shardingItemParameters){
        JobCoreConfiguration jobCoreConfiguration = JobCoreConfiguration
                .newBuilder(jobName, cron, shardingTotalCount)
                .shardingItemParameters(shardingItemParameters)
                .failover(true)
                .misfire(true)
                .build();
        SimpleJobConfiguration simpleJobConfiguration = new SimpleJobConfiguration(jobCoreConfiguration, simpleJob.getClass().getCanonicalName());
        LiteJobConfiguration liteJobConfiguration = LiteJobConfiguration.newBuilder(simpleJobConfiguration)
                .maxTimeDiffSeconds(1000)
                .monitorPort(9888)
                .overwrite(true)
                .build();
        return new SpringJobScheduler(simpleJob, regCenter, liteJobConfiguration);
    }


}
